#include "DistanceMeasurementResultWidget.h"
#include "ui_DistanceMeasurementResultWidget.h"
#include <QDebug>

DistanceMeasurementResultWidget::DistanceMeasurementResultWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::DistanceMeasurementResultWidget)
{
    ui->setupUi(this);
}

DistanceMeasurementResultWidget::~DistanceMeasurementResultWidget()
{
    delete ui;
}

void DistanceMeasurementResultWidget::updateDistanceResult(const QVariantMap &result)
{
    // 检查结果是否包含必要的数据
    if (!result.contains("measurement_mode") || !result.contains("start") ||
         !result.contains("end") || !result.contains("distance") || !result.contains("distance_unit")) {
        qWarning() << "Invalid distance measurement result: missing required fields";
        return;
    }

    // 检查是否有错误信息
    if (result.contains("error")) {
        ui->pointsLabel->setText("测量出错");
        ui->distanceLabel->setText(result.value("error").toString());
        return;
    }

    int measurementMode = result.value("measurement_mode").toInt();
    if (measurementMode == 2) {
        ui->titleLabel->setText("水平距离测量");
        ui->pointsLabel->setText(QString("左线X:%1px到右线X:%2px").arg(result.value("start").toInt()).arg(result.value("end").toInt()));
        ui->distanceLabel->setText(QString("距离:%1%2").arg(result.value("distance").toDouble()).arg(result.value("distance_unit").toString()));
     }
    else if(measurementMode == 8)
    {
        ui->titleLabel->setText("垂直距离测量");
        ui->pointsLabel->setText(QString("上线Y:%1px到下线Y:%2px").arg(result.value("start").toInt()).arg(result.value("end").toInt()));
        ui->distanceLabel->setText(QString("距离:%1%2").arg(result.value("distance").toDouble()).arg(result.value("distance_unit").toString()));
    }

}

void DistanceMeasurementResultWidget::clearResults()
{
    ui->pointsLabel->setText("从160px到445px"); // 重置为默认示例文本
    ui->distanceLabel->setText("距离285px"); // 重置为默认示例文本
    this->setVisible(false);
}
